package servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class PageGetterServlet
 * 
 * @author Sander Meijer & Stijn Kliemesch
 */
public class PageGetterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PageGetterServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		RequestDispatcher dispatcher = null;
		String getContent = request.getParameter("getContent");

		if (getContent != null) {
			boolean loggedIn = false;
			if (request.getSession().getAttribute("loggedIn") != null) {
				loggedIn = 
					(Boolean) request.getSession().getAttribute("loggedIn");
			}
			if (loggedIn
					|| request.getParameter("getContent").equals("register")) {
				if (request.getParameter("searchTarget") != null) {
					
					String searchTarget = request.getParameter("searchTarget");
					String url = "/WEB-INF/" + getContent
							+ ".jsp?searchtarget=" + searchTarget;
					if (searchTarget.equals("my-auctions")
							|| searchTarget.equals("my-bids")) {
						url += "&defaultMyAuctionContent=" + searchTarget;
					}
					dispatcher = 
						request.getRequestDispatcher(
								response.encodeURL(url));
				} else {

					dispatcher = 
						request.getRequestDispatcher(
								response.encodeURL("/WEB-INF/" + getContent + ".jsp"));
				}
			} else {
				dispatcher = 
					request.getRequestDispatcher(
							response.encodeURL("/WEB-INF/login.jsp"));
			}
			dispatcher.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}
